package test;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class RegExp {
	JFrame frame = new JFrame("边框");
	Container contentPane = null;
	JButton submitButton = new JButton("确定");
	JButton submitButton2 = new JButton("提交");
	
	ValidateThread validatedemo = new ValidateThread();
	Thread thread1 = new Thread(validatedemo);
	
	public static void main(String[] args) {
		RegExp reg = new RegExp();
		reg.getFrame();
	}

	public void getFrame() {

		final JLabel test = new JLabel("好啊");
		frame.setLocation(200, 100);
		frame.add(initUI());
		frame.setSize(600, 500);
		frame.setVisible(true);
	}

	public JButton getOK() {
		if (submitButton == null) {

			submitButton = new JButton("确定");
		}
		return submitButton;
	}

	public JPanel initUI() {
		final JTextField mail = new JTextField("输入邮箱", 20);

		Font font = new Font("Serief", Font.BOLD, 22);

		final JLabel right = new JLabel("恭喜可用(,)");
		final JLabel wrong = new JLabel("不可用");
		right.setFont(font);
		right.setForeground(Color.RED);// 设置标签前景色
		final JPanel jpanel = new JPanel();
		jpanel.add(getOK());
		jpanel.add(mail);
		jpanel.add(submitButton2);
		jpanel.setSize(60, 80);
		jpanel.add(right);
		// submitButton2.setEnabled(false);
		submitButton2.addFocusListener(new FocusListener() {

			@Override
			public void focusGained(FocusEvent e) {
				System.out.println("获得焦点");
				
				thread1.start();
			}

			@Override
			public void focusLost(FocusEvent e) {
				System.out.println("失去焦点");
				thread1.interrupt();
			}
		});

		submitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String email = mail.getText();
				String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
				if (match(regex, email)) {
					// TODO
					System.out.println("恭喜邮箱可用");
					// jpanel.remove(mail);
					// frame.add(initUI());
					// frame.validate();
					// frame.repaint();
				} else {
					System.out.println("邮箱不能用");
					// // jpanel.add(submitButton);
					// jpanel.add(wrong);
					// // jpanel.remove(submitButton);
					// jpanel.remove(mail);
					// frame.add(initUI());
					// frame.validate();
					// frame.repaint();
				}

			}
		});

		mail.addKeyListener(new KeyListener() {

			public void keyPressed(KeyEvent e) {

			}

			@Override
			public void keyReleased(KeyEvent e) {

			}

			@Override
			public void keyTyped(KeyEvent e) {

			}

		});

		return jpanel;
	}

	private static boolean match(String regex, String str) {
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.matches();
	}

	class ValidateThread implements Runnable {

		public void run() {

			synchronized (this) {
				while (true) {
	
		
					try {
						Thread.sleep(5000);
						System.out.println("每隔3秒开始运行");
					} catch (InterruptedException e) {
						return;
					}
				}

			}

		}

	}
}
